#include "city.h"
#include "code.h"
#include <iostream>
#include <fstream>
#include <typeinfo>
#include <cstdlib>
#include <algorithm>
#define P 10000
#define random() rand()%(P)/(float)(P)

city* random_city(int n);
void put_out_city(city* C ,int _l);
city* load_city(int _l);
city* load_rcity(int _l);
code* random_DNA(int n, int _l);
void put_out(code DNA);
void put_out2(code DNA);

city* random_city(int n){
  city* C=new city[n];
  double x,y;
  for (int i=0; i<n ; i++){
    x=random();
    y=random();
    C[i].set_city(x,y);
  }
  return C;
}

void put_out_city(city* C ,int _l){
  std::ofstream ofile("rcity.txt");
  for (int i=0; i<_l ; i++){
    ofile << C[i].get_x() << " ";
    ofile << C[i].get_y() << " ";
  }
  ofile.close();
}

city* load_city(int _l){
  city* C=new city[_l];
  double x,y;
  std::ifstream infile;
  infile.open("city.txt");
  for (int i=0; i<_l ; i++){
    infile >> x;
    infile >> y;
    C[i].set_city(x,y);
  }
  infile.close();
  return C;
}

city* load_rcity(int _l){
  city* C=new city[_l];
  double x,y;
  std::ifstream infile;
  infile.open("rcity.txt");
  for (int i=0; i<_l ; i++){
    infile >> x;
    infile >> y;
    C[i].set_city(x,y);
  }
  infile.close();
  return C;
}


code* random_DNA(int n, int _l){
  code* DNA=new code[n];
  for (int i=0; i<n ; i++){
    int* A=new int[_l];
    for (int j=0; j<_l; j++)
      A[j]=j;
    std::random_shuffle(A,A+_l);
    code D(A,_l);
    DNA[i]=D;
  }
  return DNA;
}

void put_out(code DNA){
  std::ofstream ofile("result1.txt");
  int leng=DNA.get_length();
  for (int i=0; i<leng ; i++)
    ofile << *(DNA.get_array()+i) << " ";
  ofile.close();
}

void put_out2(code DNA){
  std::ofstream ofile("result2.txt");
  int leng=DNA.get_length();
  for (int i=0; i<leng ; i++)
    ofile << *(DNA.get_array()+i) << " ";
  ofile.close();
}
